package com.shanzmoo.base.bean;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Collections;
import java.util.List;

/**
 * 分页响应类
 * @author: Gzhao 2020/11/12
 */
@Data
@Accessors(chain = true)
@ApiModel("分页响应")
public class PageRespBase<T> {
    @ApiModelProperty(name = "totalPage", value = "总页数")
    private Long totalPage;

    @ApiModelProperty(name = "totalCount", value = "总记录数")
    private Long totalCount;

    @ApiModelProperty(name = "currPage", value = "当前页")
    private Long currPage;

    @ApiModelProperty(name = "pageSize", value = "当前页的数量")
    private Long pageSize;

    @ApiModelProperty(name = "list", value = "结果集")
    private List<T> list;

    public static PageRespBase create(IPage page, List list) {
        return new PageRespBase<>().setTotalPage(page.getPages())
                .setTotalCount(page.getTotal())
                .setCurrPage(page.getCurrent())
                .setPageSize(page.getSize())
                .setList(list);
    }
    public static PageRespBase create(IPage page) {
        return new PageRespBase<>().setTotalPage(page.getPages())
                .setTotalCount(page.getTotal())
                .setCurrPage(page.getCurrent())
                .setPageSize(page.getSize())
                .setList(page.getRecords());
    }

    public static PageRespBase create(Long totalCount, Long currPage, Long pageSize, List list) {
        long totalPage = totalCount/pageSize + 1;
        if(totalCount%pageSize == 0){
            totalPage = totalCount/pageSize;
        }
        return new PageRespBase<>()
                .setTotalPage(totalPage)
                .setTotalCount(totalCount)
                .setCurrPage(currPage)
                .setPageSize(pageSize)
                .setList(list);
    }

    public static PageRespBase empty(Long pageSize) {
        return create(0L, 1L, pageSize, Collections.emptyList());
    }
}
